package com.fuzhixuan.springcloud.controller;

import com.fuzhixuan.cloud.entities.CommonCode;
import com.fuzhixuan.cloud.entities.CommonResult;
import com.fuzhixuan.cloud.entities.Payment;

import com.fuzhixuan.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class PaymentController {
    @Autowired
    private PaymentService paymentService;
    @Value("${server.port}")
    private String serverPort;

    @GetMapping(value = "/user-api/payment/getById")
    public CommonResult<Payment> getById(Integer id) {
        Payment result = paymentService.getPaymentById(id);
        return CommonResult.ok(CommonCode.SUCCESS_CODE, "serverPort=" + serverPort + "," + result);
    }

    @PostMapping(value = "/user-api/payment/insert")
    public CommonResult<Payment> insert(@RequestBody Payment payment) {
        int result = paymentService.create(payment);
        if (result > 0) {
            return CommonResult.ok(CommonCode.SUCCESS_CODE, "serverPort=" + serverPort + "," + result);
        } else {
            return CommonResult.fail(CommonCode.FAILURE_CODE, "serverPort=" + serverPort + "," + "数据插入失败");
        }
    }

    @PostMapping(value = "/open-api/payment/update")
    public CommonResult<Payment> update(@RequestBody Payment payment) {
        int result = paymentService.update(payment);
        if (result > 0) {
            return CommonResult.ok(CommonCode.SUCCESS_CODE, "serverPort=" + serverPort + "," + result);
        } else {
            return CommonResult.fail(CommonCode.FAILURE_CODE, "serverPort=" + serverPort + "," + "数据更新失败");
        }
    }

    @GetMapping(value = "/open-api/payment/getPort")
    public Object getPort() {
        return serverPort;
    }
}
